import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class LC_349 {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set = new HashSet<>();
        int[] arr = new int[nums2.length];
        for(int i = 0; i < nums1.length; i++){
            if(set.contains(nums1[i]) == false){
                set.add(nums1[i]);
            }
        }
        int count = 0;
        for(int i = 0; i < nums2.length; i++){
            if(set.contains(nums2[i]) == true){
                arr[count++] = nums2[i];
                set.remove(nums2[i]);
            }
        }
        return Arrays.copyOfRange(arr, 0, count);
    }
}
